<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增权限' : '编辑权限'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="公司编号" prop="compNo">
        <el-input v-model="form.compNo" style="width: 360px;"/>
      </el-form-item>
      <el-form-item label="公司名称" prop="compName">
        <el-input v-model="form.compName" style="width: 360px;"/>
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="公司类型">
        <el-select v-model="form.compTypeName" placeholder="请选择" style="width: 360px;" @change="changeType">
          <el-option
            v-for="(item, i) in permissions"
            :key="i"
            :label="item.dictName"
            :value="item.dictNo"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公司级别" prop="compLevel">
        <el-select v-model="form.compLevel" placeholder="请选择" style="width: 360px;" @change="changeLevel">
          <el-option
            v-for="(item, i) in companyLevels"
            :key="i"
            :label="item.dictName"
            :value="item.dictNo"/>
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="contactAddr">
        <el-input v-model="form.contactAddr" style="width: 360px;"/>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="form.tel" style="width: 360px;"/>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  // components: { Treeselect },
  props: {
    permissions: {
      type: Array,
      required: true
    },
    companyLevels: {
      type: Array,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  inject: ['Edit'],
  data() {
    return {
      loading: false, dialog: false,
      form: { compName: '', compTypeName: '', compLevel: "", recvAddress: "", tel: ""},
      value: "INTERNAL",
      compType: "",
      compLevel: "",
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        alias: [
          { required: true, message: '请输入别名', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    changeType(e) {
      console.log(e)
      this.compType = e
    },
    changeLevel(e) {
      this.compLevel = e
    },
    doSubmit() {
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      // debugger/
      let formData = Object.assign({}, this.form, { compType: this.compType, compLevel: this.compLevel })
      this.$api.fenxiao.company.create({
        data: formData
      }).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
        // this.$parent.$parent.getPermissions()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      let obj = Object.assign({}, this.form, { id: this.Edit.uid }, { compType: this.compType })
      this.$api.fenxiao.company.update({
        data: obj
      }).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
        this.sup_this.getPermissions()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { name: '', alias: '', pid: 0 }
    }
  }
}
</script>

<style scoped>

</style>
